<template>
  <div>
    <div class="caretaker">
      <!-- 间距组件 -->
      <div class="space">
        <el-space>
          <el-input
            v-model.trim="username"
            @keyup.enter="careTaker()"
            placeholder="请输入管理员名称"
          />
          <el-button
            type="primary"
            class="iconfont icon-sousuo"
            @click="careTaker()"
            >查找</el-button
          >
          <el-button
            type="primary"
            class="iconfont icon-jishiben"
            @click="add()"
            >添加</el-button
          >
        </el-space>
      </div>
      <!-- 表格组件 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="管理员ID" />
        <el-table-column prop="username" label="管理员名称" />
        <el-table-column prop="realname" label="真实姓名" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="roleIds" label="管理员角色">
          <template #default="scope">
            <el-tag v-for="(item, index) in scope.row.roleIds" :key="index">
              {{ opps(item) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 对话框 -->
      <el-dialog
        v-model="dialogVisible"
        title="创建"
        width="30%"
        destroy-on-close
      >
        <el-form
          ref="elfroma"
          status-icon
          :rules="rules"
          label-width="100px"
          :model="show"
          class="heads"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="show.username" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="show.password"
              type="password"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="show.phone" />
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="show.realname" />
          </el-form-item>
          <el-form-item label="角色" prop="roleIds">
            <el-select v-model="show.roleIds" placeholder="请选择角色" multiple>
              <el-option
                v-for="item in rold"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button type="primary" @click="addEd('elfroma')">新增</el-button>
            <el-button @click="addNnll()">重置</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 编辑 -->
      <!-- 编辑管理员 -->
      <el-dialog v-model="editor" title="修改" width="30%">
        <el-form
          ref="elfromb"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="100px"
          class="heads"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="ruleForm.username"
              type="text"
              autocomplete="off"
              disabled
            />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" />
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="ruleForm.realname" />
          </el-form-item>
          <el-form-item label="角色" prop="roleIds">
            <el-select
              v-model="ruleForm.roleIds"
              placeholder="请选择角色"
              multiple
            >
              <el-option
                v-for="item in rold"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button type="primary" @click="Modifies('elfromb')"
              >新增</el-button
            >
            <el-button @click="editora('elfromb')">重置</el-button>
          </span>
        </template>
      </el-dialog>
      <PagLnatio
        v-bind="paging"
        @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange"
      ></PagLnatio>
    </div>
  </div>
</template>

<script>
import PagLnatio from "@/components/PagLnatio.vue";
import http from "@/http";
import { ElMessage as $msg } from "element-plus";
export default {
  components: {
    PagLnatio,
  },
  data() {
    return {
      tableData: [], //表格数据
      rold: [], //枚举数据
      username: "",
      dialogVisible: false,
      editor: false,
      // id: "",
      // value: "",
      paging: {
        page: 1, //默认第一页
        limit: 10, //显示几页
        total: null,
      },

      //添加
      show: {
        username: "",
        password: "",
        phone: "",
        realname: "",
        roleIds: [],
      },
      //修改
      ruleForm: {
        id: "",
        username: "",
        realname: "",
        phone: "",
        roleIds: [],
      },

      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空！",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空！",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号！",
            trigger: "blur",
          },
          {
            pattern: /^1\d{10}$/,
            message: "手机格式要11位",
            trigger: "blur",
          },
        ],
        realname: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
          {
            pattern: /^[\u4e00-\u9fa5]*$/,
            message: "真实姓名必须是中文",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.careTaker();
    this.appe();
  },
  methods: {
    //渲染列表
    careTaker() {
      let data = {
        _gp: "admin",
        _mt: "list",
        username: this.username,
        page: this.paging.page,
        limit: this.paging.limit,
      };
      http.get(data).then((res) => {
        console.log(res);
        if (200 == res.errno) {
          this.paging.total = res.data.total;
          this.tableData = res.data.items;
        }
      });
    },
    //枚举数据接口
    appe() {
      let ass = {
        _gp: "admin.role",
        _mt: "options",
      };
      http.post(ass).then((res) => {
        console.log(res);
        this.rold = res.data;
      });
    },
    opps(row) {
      // console.log(row);
      for (const items of this.rold) {
        if (row == items.value) {
          return items.label;
        }
      }
    },
    //添加对话框显示
    add() {
      this.dialogVisible = true;
    },
    addEd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let odd = {
            _gp: "admin",
            _mt: "create",
            adminDTO: JSON.stringify(this.show),
          };
          http.post(odd).then((res) => {
            console.log(res);
            if (200 == res.errno) {
              ElMessage.success("添加管理员成功");
              this.dialogVisible = false;
              this.careTaker();
              this.show = this.$options.data().show;
            } else {
              ElMessage.error("用户名已重复");
            }
          });
        } else {
          console.log("参数校验失败！");
          return false;
        }
      });
    },
    addNnll() {
      this.show = {
        phone: "",
        realname: "",
        roleIds: [],
      };
    },
    editora(row) {
      this.$refs[row].resetFields();
    },

    //编辑
    handleEdit(index, row) {
      this.editor = true;
      console.log(row);
      Object.assign(this.ruleForm, row);
      // this.ruleForm.password = row.password
    },
    //修改
    Modifies(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let bianji = {
            _gp: "admin",
            _mt: "edit",
            adminDTO: JSON.stringify(this.ruleForm),
          };
          http.post(bianji).then((res) => {
            console.log(res);
            if (200 == res.errno) {
              ElMessage.success("修改成功");
              this.editor = false;
              this.careTaker();
            }
          });
        } else {
          console.log("参数校验失败！");
          return false;
        }
      });
    },
    //删除管理员
    handleDelete(index, row) {
      console.log(row);
      ElMessageBox.confirm("是否删除此管理员", {
        confirmButtonText: "OK",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let deletes = {
            _gp: "admin",
            _mt: "delete",
            id: row.id,
          };
          http.post(deletes).then((res) => {
            console.log(res);
            if (200 == res.errno) {
              this.tableData.splice(index, 1);
              this.careTaker();
            }
          });
          ElMessage({
            type: "success",
            message: "删除成功",
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消成功",
          });
        });
    },
    //分页
    handleSizeChange(val) {
      console.log(val);
      this.paging.limit = val;
      this.careTaker();
    },
    handleCurrentChange(val) {
      this.paging.page = val;
      this.careTaker();
    },
  },
};
</script>

<style lang="less" scope>
.caretaker {
  width: 98%;
  margin: auto;
  .space {
    height: 40px;
  }
}
</style>
